<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商城产品信息')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-edit" th:object="${jiajiaMallShopProductInformation}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">店铺名称：</label>
                <div class="col-sm-8">
                    <input  id="shopId" name="shopId" class="form-control" type="hidden"  required>
                    <input   id="shopName" th:field="*{shopName}" name="shopName" class="form-control" type="text" onclick="openUser()"  required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">产品名称：</label>
                <div class="col-sm-8">
                    <input name="productName" th:field="*{productName}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">产品大类：</label>
                <div class="col-sm-8">
                    <select id="typeId" class="form-control" name="typeId">
                        <option value="">请产品大类</option>
                        <option th:each="type:${productTypeList}" th:value="${type.id}" th:text="${type.productType}" th:field="*{typeId}"></option>
                    </select>
                </div>


            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">产品小类：</label>
                <div class="col-sm-8">
                    <select id="levelId"  class="form-control" name="levelId" >
                        <option value="">请选择产品小类</option>
                        <option th:each="level:${productLevelList}" th:value="${level.id}" th:text="${level.productLevel}" th:field="*{levelId}"></option>
                    </select>

                </div>

            </div>


<!--            <div class="form-group">
                <label class="col-sm-3 control-label">产品图片ids：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="file" class="form-control" accept="image/jpg,image/jpeg,image/png" name="file"  type="file">
                        <img th:src="*{photoOssIds}" style="width: 80px ;height: auto;" src="" id="imgPhoto"  />
                        <input th:field="*{photoOssIds}" id="photoOssIds" class="form-control" name="photoOssIds"   type="text" style="display:none">
                    </div>

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">产品图片id：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="file2" class="form-control" accept="image/jpg,image/jpeg,image/png" name="file"  type="file">
                        <img th:src="*{photoOssId}" style="width: 80px ;height: auto;" src="" id="imgPhoto2"  />
                        <input th:field="*{photoOssId}" id="photoOssId" class="form-control" name="photoOssId"   type="text" style="display:none">
                    </div>
                </div>
            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">产品轮播图：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="file3" class="form-control" accept="image/jpg,image/jpeg,image/png" name="file" multiple type="file">
                        <img th:src="*{photoUrlIds}" style="width: 80px ;height: auto;" src="" id="imgPhoto3"  />
                        <input th:field="*{photoUrlIds}" id="photoUrlIds" class="form-control" name="photoUrlIds"   type="text" style="display:none">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">产品图片：</label>
                <div class="col-sm-8">
                    <div class="file-loading">
                        <input id="file4" class="form-control" accept="image/jpg,image/jpeg,image/png" name="file"  type="file">
                        <img th:src="*{photoUrlId}" style="width: 80px ;height: auto;" src="" id="imgPhoto4"  />
                        <input th:field="*{photoUrlId}" id="photoUrlId" class="form-control" name="photoUrlId"   type="text" style="display:none">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">产品描述：</label>
                <div class="col-sm-8">
                    <input type="hidden" class="form-control" th:field="*{productDescribe}">
                    <div class="summernote" id="productDescribe"></div>
                </div>


            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">产品产地：</label>
                <div class="col-sm-8">
                    <input name="productOrigin" th:field="*{productOrigin}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">产品质量：</label>
                <div class="col-sm-8">
                    <input name="productQuality" th:field="*{productQuality}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">产品价格：</label>
                <div class="col-sm-8">
                    <input name="productPrice" th:field="*{productPrice}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">产品规格：</label>
                <div class="col-sm-8">
                    <input name="specifications" th:field="*{specifications}" class="form-control" type="text">

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">库存：</label>
                <div class="col-sm-8">
                    <input name="inventory" th:field="*{inventory}" class="form-control" type="text">
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />

    <script th:inline="javascript">
        var prefix = ctx + "mall/product";
        $("#form-product-edit").validate({
            focusCleanup: true
        });

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: "/common/admin/upload/up/single",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });

/*        $("#file").bind('change', function () {
            $("#imgPhoto").show();
            $("#imgPhoto").attr("src",URL.createObjectURL($(this)[0].files[0]));
            uploadFile("#file",function (res1) {
                $("#photoOssIds").val(res1.url);
            })
        });
        $("#file2").bind('change', function () {
            $("#imgPhoto2").show();
            $("#imgPhoto2").attr("src",URL.createObjectURL($(this)[0].files[0]));
            uploadFile("#file2",function (res1) {
                $("#photoOssId").val(res1.url);
            })
        });*/
        $("#file3").bind('change', function () {
            $("#imgPhoto3").show();
            $("#imgPhoto3").attr("src",URL.createObjectURL($(this)[0].files[0]));
            uploadFiles("#file3",function (res1) {

                $("#photoUrlIds").val(res1.files_url);
            })
        });
        $("#file4").bind('change', function () {
            $("#imgPhoto4").show();
            $("#imgPhoto4").attr("src",URL.createObjectURL($(this)[0].files[0]));
            uploadFile("#file4",function (res1) {
                $("#photoUrlId").val(res1.url);
            })
        });
        function uploadFile(idSelector,callback) {
            var formData = new FormData();
            formData.append('file',$(idSelector)[0].files[0]);
            $.modal.loading("图片上传中，请稍后...")
            $.ajax({
                url: "/common/admin/upload/up/single",
                data: formData,
                type: "post",
                dataType: "json",
                processData: false,
                contentType: false,
                success: function(result) {
                    $.modal.closeLoading();
                    callback(result)
                }
            })
        }

        function uploadFiles(idSelector,callback) {
            var formData = new FormData();
            var files = $(idSelector)[0].files;
            for (var i = 0; i < files.length; i++) {
                formData.append('files', files[i]); // 将文件添加到FormData对象中
            }
            $.modal.loading("图片上传中，请稍后...")
            $.ajax({
                url: "/common/admin/upload/up/more",
                data: formData,
                type: "post",
                dataType: "json",
                processData: false,
                contentType: false,
                success: function(result) {
                    $.modal.closeLoading();
                    callback(result)
                }
            })
        }

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-product-edit').serialize());
            }
        }

        $("input[name='version']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("#typeId").change(function () {//同上面一样
            $.ajax({
                url: ctx + "mall/level/select",
                type: 'GET',
                data: {
                    typeId: $("#typeId").val(),
                },
                success:function (data) {
                    $("#levelId").empty();
                    $("#levelId").append("<option value=''>请产品小类</option>");
                    for (var i = 0; i < data.length; i++){
                        $("#levelId").append("<option value='" + data[i].id + "'>" + data[i].productLevel + "</option>");
                    }
                }
            })
        });


        function openUser(){
            var options = {
                title: '请选择店铺',
                url: "/mall/shop/childShop",
                callBack: getMember
            };
            $.modal.openOptions(options);
        }


        // 回调，将子页面用户选择的成员返回父页面
        function getMember(index, layero){
            var row = layero.find("iframe")[0].contentWindow.getSelections();
            if (row.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }

            // 这里已经拿到了Members，即子页面返回的表格勾选的内容
            $("#shopId").val(row[0].id);
            $("#shopName").val(row[0].shopName);
            $.modal.close(index);
            $.modal.closeLoading();

        }

    </script>
</body>
</html>